<template>
  <div>
    <el-form ref="form" :model="form" inline>
      <el-form-item label="辅导员姓名" style="font-weight:bold;">
        <el-input v-model="form.adminName" style="width:150px" placeholder="请输入姓名" clearable></el-input>
      </el-form-item>
      <el-form-item label="辅导员工号" style="font-weight:bold;">
        <el-input v-model="form.adminNum" style="width:150px" placeholder="请输入工号" clearable></el-input>
      </el-form-item>
      <el-form-item label="年级" style="font-weight:bold;">
        <el-input v-model="form.grade" style="width:150px" placeholder="(如2021级)" clearable></el-input>
      </el-form-item>
      <el-form-item label="学生类型" style="font-weight:bold;">
        <el-select v-model="form.stuType" placeholder="请选择学生类型" style="width:150px;" clearable>
          <el-option label="本科生" value="0"></el-option>
          <el-option label="硕士生" value="1"></el-option>
          <el-option label="博士生" value="2"></el-option>
          <el-option label="留学生" value="3"></el-option>
          <el-option label="交换生" value="4"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="院系" style="font-weight:bold;">
        <el-select v-model="form.collegeId" placeholder="请选择院系" style="width:150px;" :disabled="disabled" clearable>
          <el-option v-for="item in CollegeName" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
    </el-form>

    <div class="buttonList">
      <el-button type="primary" size="small" icon="el-icon-folder-add" @click="dialog_newly = true">新增</el-button>
      <!-- <el-upload action="#" :before-upload="beforeUpload" ref="newuploadin" :show-file-list="false"
        :http-request="uploadHttpRequest" :limit="3" :file-list="fileList">
        <el-button size="small" type="success" plain icon="el-icon-upload2">上传</el-button>
      </el-upload>
      <el-button size="small" type="success" plain icon="el-icon-download" @click="exportClass">下载模板</el-button> -->
      <div class="buttonList_right">
        <el-button size="small" class="search_button" type="primary" icon="el-icon-search" @click="getForm">查询</el-button>
        <el-button size="small" class="reset_button" icon="el-icon-refresh" @click="reset">重置</el-button>
      </div>
    </div>

    <el-divider></el-divider>


    <el-table :data="tableData" border size="medium">
      <el-table-column prop="collegeName" label="院系" align="center">
      </el-table-column>
      <el-table-column prop="grade" label="年级" align="center">
      </el-table-column>
      <el-table-column prop="classes" label="班级" align="center">
      </el-table-column>
      <el-table-column prop="stuType" label="学生类型" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.stuType == 0">本科生</span>
          <span v-if="scope.row.stuType == 1">硕士生</span>
          <span v-if="scope.row.stuType == 2">博士生</span>
          <span v-if="scope.row.stuType == 3">留学生</span>
          <span v-if="scope.row.stuType == 4">交换生</span>
        </template>
      </el-table-column>
      <el-table-column prop="adminName" label="辅导员姓名" align="center">
      </el-table-column>
      <el-table-column prop="adminNum" label="辅导员工号" align="center">
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" align="center">
      </el-table-column>
      <el-table-column prop="" label="操作" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit-outline"
            @click="changeClassDialog(scope.row)">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="deleteClass(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination class="pagination" background layout="total, sizes, prev, pager, next, jumper" :total="total"
      :page-size="10" :page-sizes="[10, 20, 30, 40]" @current-change="handleCurrentChange"
      @size-change="handleSizeChange"></el-pagination>

    <!-- 添加弹窗 -->
    <el-dialog title="添加班级信息" :visible.sync="dialog_newly">
      <el-form label-position="left" label-width="100px" style="margin-left:20%;">
        <el-form-item label="辅导员姓名" style="font-weight:bold;">
          <el-input v-model="formNew.adminName" style="width:200px" placeholder="请输入姓名"></el-input>
        </el-form-item>
        <el-form-item label="辅导员工号" style="font-weight:bold;">
          <el-input v-model="formNew.adminNum" style="width:200px" placeholder="请输入工号"></el-input>
        </el-form-item>
        <el-form-item label="班级" style="font-weight:bold;">
          <el-input v-model="formNew.classes" style="width:200px" placeholder="请输入班级"></el-input>
        </el-form-item>
        <el-form-item label="年级" style="font-weight:bold;">
          <el-input v-model="formNew.grade" style="width:200px" placeholder="请输入年级"></el-input>
        </el-form-item>
        <el-form-item label="学生类型" style="font-weight:bold;">
          <el-select v-model="formNew.stuType" placeholder="请选择学生类型" style="width:200px;" clearable>
            <el-option label="本科生" value="0"></el-option>
            <el-option label="硕士生" value="1"></el-option>
            <el-option label="博士生" value="2"></el-option>
            <el-option label="留学生" value="3"></el-option>
            <el-option label="交换生" value="4"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="院系" style="font-weight:bold;">
          <el-select v-model="formNew.collegeId" :disabled="disabled" placeholder="请选择院系" style="width:200px;" clearable>
            <el-option v-for="item in CollegeName" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialog_newly = false">取 消</el-button>
        <el-button type="primary" @click="addClass" class="dialogbutton">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 添加弹窗 -->

    <!-- 修改弹窗 -->
    <el-dialog title="修改班级信息" :visible.sync="dialog_change">
      <el-form label-position="left" label-width="100px" style="margin-left:20%;">
        <!-- <el-form-item label="辅导员姓名" style="font-weight:bold;">
          <el-input v-model="formChange.adminName" style="width:200px" placeholder="请输入姓名"></el-input>
        </el-form-item>
        <el-form-item label="辅导员工号" style="font-weight:bold;">
          <el-input v-model="formChange.adminNum" style="width:200px" placeholder="请输入工号"></el-input>
        </el-form-item> -->
        <el-form-item label="班级" style="font-weight:bold;">
          <el-input v-model="formChange.className" style="width:200px" placeholder="请输入班级"></el-input>
        </el-form-item>
        <el-form-item label="年级" style="font-weight:bold;">
          <el-input v-model="formChange.grade" style="width:200px" placeholder="请输入年级"></el-input>
        </el-form-item>
        <el-form-item label="学生类型" style="font-weight:bold;">
          <el-select v-model="formChange.type" placeholder="请选择学生类型" style="width:200px;" clearable>
            <el-option label="本科生" value="0"></el-option>
            <el-option label="硕士生" value="1"></el-option>
            <el-option label="博士生" value="2"></el-option>
            <el-option label="留学生" value="3"></el-option>
            <el-option label="交换生" value="4"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="辅导员姓名" style="font-weight:bold;">
          <el-input v-model="formChange.adminName" style="width:200px" placeholder="请输入姓名"></el-input>
        </el-form-item>
        <el-form-item label="辅导员工号" style="font-weight:bold;">
          <el-input v-model="formChange.adminNum" style="width:200px" placeholder="请输入工号"></el-input>
        </el-form-item>
        <!-- <el-form-item label="院系" style="font-weight:bold;">
          <el-select v-model="formChange.collegeId" placeholder="请选择院系" style="width:200px;" clearable>
            <el-option v-for="item in CollegeName" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialog_change = false">取 消</el-button>
        <el-button type="primary" class="dialogbutton" @click="changeClassConfirm">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 修改弹窗 -->
  </div>
</template>

<script>
import { getCollegeName } from '_api/DormManage/College/index.js'
import { getList, addClass, uploadClass, exportClass, changeClass, removeClass } from '_api/DormManage/ClassRoomManage/index.js'
export default {
  data() {
    return {
      dialog_newly: false,
      dialog_change: false,
      form: {
        current: 0,
        pageSize: 10,
        adminName: "",
        collegeId: "",
        grade: "",
        stuType: "",
        classes: "",
        adminNum: ""
      },
      formNew: {
        adminName: "",
        collegeId: "",
        grade: "",
        stuType: "",
        classes: "",
        adminNum: ""
      },
      formChange: {
        id: "",
        adminName: "",
        // collegeId: "",
        grade: "",
        type: "",
        className: "",
        adminNum: ""
      },
      tableData: [],
      total: 0,
      CollegeName: [],
      fileList: [],
      timeNow: "",
      disabled: false
    }
  },

  created() {
    this.getCollegeName()
  },
  methods: {
    changeClassDialog(e) {
      this.formChange.adminName = e.adminName
      this.formChange.adminNum = e.adminNum
      this.formChange.id = e.id
      this.formChange.grade = e.grade
      this.formChange.className = e.classes
      this.formChange.type = e.stuType.toString()
      // this.CollegeName.forEach(item => {
      //   if (item.name == e.collegeName) {
      //     this.formChange.collegeId = item.id
      //   }
      // })
      this.dialog_change = true
    },
    changeClassConfirm() {
      changeClass(this.formChange).then(res => {
        if (res.data.success == true) {
          this.dialog_change = false
          this.$message({
            message: '修改成功!',
            duration: 1000,
            type: 'success',
            onClose: () => {
              this.reset()
            }
          })
        } else {
          this.$message(res.data.message)
        }
      })
    },
    deleteClass(e) {
      this.$confirm('此操作将删除该班级, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        removeClass({ id: e }).then(res => {
          if (res.data.success == true) {
            this.$message({
              message: '删除成功!',
              duration: 1000,
              type: 'success',
              onClose: () => {
                this.reset()
              }
            })
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    //获取当前时间
    getCurrentTime() {
      var _this = this;
      let yy = new Date().getFullYear();
      let mm = new Date().getMonth() + 1;
      let dd = new Date().getDate();
      let hh = new Date().getHours();
      let mf = new Date().getMinutes() < 10 ? '0' + new Date().getMinutes() : new Date().getMinutes();
      let ss = new Date().getSeconds() < 10 ? '0' + new Date().getSeconds() : new Date().getSeconds();
      _this.gettime = yy + '-' + mm + '-' + dd + ' ' + hh + ':' + mf + ':' + ss;
      // console.log(_this.gettime)
      this.timeNow = _this.gettime
    },
    // 导出模板
    exportClass() {
      exportClass().then(res => {
        const link = document.createElement('a')  //创建a标签
        const blob = new Blob([res.data], { type: 'application/vnd.ms-excel' }) //设置文件流
        link.style.display = 'none'// 设置连接
        link.href = URL.createObjectURL(blob)  //将文件流转化为blob地址
        this.getCurrentTime()
        link.download = '班级管理模板_' + this.timeNow
        document.body.appendChild(link) // 模拟点击事件
        link.click()  //设置点击事件
        this.$message.success('导出成功！')
      })
    },
    // 导入
    uploadHttpRequest(data) {
      let fd = new FormData()
      fd.append('file', data.file)
      uploadClass(fd).then(res => {
        if (res.data.success == true) {
          this.$message.success("导入成功！")
          this.reset()
        } else {
          this.$message({
            message: res.data.message,
            duration: 3000
          })
        }
      })

    },
    // 上传校验
    beforeUpload(file) {
      // console.log(file)
      let legalName = ["xlsx", "xls"];
      // 拿到后缀名
      let name = file.name.substring(
        file.name.lastIndexOf(".") + 1,
        file.name.length
      );
      if (legalName.includes(name)) {
      } else {
        this.$message.info("文件格式不对,仅限xls和xlsx");
        return false;
      }
    },
    // 增加
    addClass() {
      addClass(this.formNew).then(res => {
        if (res.data.success == true) {
          this.dialog_newly = false
          this.$message({
            message: '添加成功!',
            duration: 1000,
            type: 'success',
            onClose: () => {
              this.reset()
            }
          })
        } else {
          this.$message(res.data.message)
        }
      })
    },
    //重置
    reset() {
      this.form = {
        current: 0,
        pageSize: 10,
        adminName: "",
        collegeId: "",
        grade: "",
        stuType: "",
        classes: ""
      }
      this.getCollegeName()
    },
    //当前页
    handleCurrentChange(e) {
      this.form.current = (e - 1) * 10
      this.getForm()
    },
    handleSizeChange(e) {
      this.form.pageSize = e
      this.getForm()
    },
    //获取学院名称
    getCollegeName() {
      getCollegeName().then(res => {
        if (res.status == 200) {
          this.CollegeName = res.data.data
          if (window.localStorage.getItem('clientRole') == 2) {
            this.CollegeName.forEach(item => {
              if (item.name == window.localStorage.getItem('collegeName')) {
                this.form.collegeId = item.id
                this.formNew.collegeId = item.id
                this.disabled = true
              }
            })
          }
          this.getForm()
        }
      })
    },
    //查询列表信息
    getForm() {
      getList(this.form).then(res => {
        if (res.data.success == true) {
          this.tableData = res.data.data.list
          this.total = res.data.data.total
        } else {
          this.tableData = res.data.data
          this.total = ""
          this.$message.info(res.data.message)
        }
      })
    },
  }
}
</script>

<style lang="scss" scoped>
.buttonList {
  margin-left: 20px;
  display: flex;
  width: 80%;
  justify-content: space-between;
}

.buttonList_right {}

.pagination {
  float: right;
  margin-top: 10px;
}
</style>